﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
namespace webnew.BLL
{
    /// <summary>
    /// 业务逻辑类 Sys_Staff
    /// </summary>
    public class Sys_Staff
    {
        private readonly webnew.DAL.Sys_StaffHelper dal = new webnew.DAL.Sys_StaffHelper();
        public Sys_Staff()
        { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(webnew.Model.Sys_StaffInfo model)
        {
            CheckModel(model);
            return dal.Add(model);
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Modify(webnew.Model.Sys_StaffInfo model)
        {
            CheckModel(model);
            return dal.Modify(model);
        }

        /// <summary>
        /// 根据ID获取一个实体
        /// </summary>
        public Model.Sys_StaffInfo GetModel(string id)
        {
            return dal.GetModel(id);
        }

   
        /// <summary>
        /// 取得数据行数,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public int GetCount(string strTable, string condition)
        {
            return dal.GetCount(strTable, condition);
        }


        public SqlDataReader GetPageList(string field, string condition, int pageSize, int pageIndex, string tbNames, string sortNames, bool sortType)
        {
            if (pageSize <= 0)
                throw new Exception("每页数据条数必须大于0。");

            if (pageIndex <= 0)
                throw new Exception("页索引必须大于0。");

            if (string.IsNullOrEmpty(sortNames))
                throw new Exception("排序字段不能为空。如果是默认排序，可以填写主键字段。");
            return dal.GetPageList(field, condition, pageSize, pageIndex, tbNames, sortNames, sortType);
        }


        /// <summary>
        /// 自动生成员工编号
        /// </summary>
        /// <returns></returns>
        public string GetNewStaffNO()
        {
            return dal.GetNewStaffNO();
        }

        /// <summary>
        /// 根据员工编号 查找数据库是否存在该员工编号
        /// </summary>
        /// <param name="strStaffNO">员工编号</param>
        /// <returns>返回string "1"代表存在,"0"代表不存在</returns>
        public string ExistStaffNO(string strStaffNO)
        {
            return dal.ExistStaffNO(strStaffNO);
        }

        /// <summary>
        /// 导出Excel获取列表数据(带条件的)
        /// </summary>
        public SqlDataReader GetExcelList(string strFeild, string strTable, string condition)
        {
            return dal.GetExcelList(strFeild, strTable, condition);
        }

        #region 验证参数的有效性，请在此加入更多业务逻辑的验证

        /// <summary>
        /// 验证实体的有效性
        /// </summary>
        private void CheckModel(webnew.Model.Sys_StaffInfo model)
        {
            if (model == null)
                throw new ArgumentNullException("参数不能为空。");

            if (!Regex.IsMatch(model.TrueName, @"^[\s\w\u4e00-\u9fa5]{1,50}$"))
                throw new ArgumentException("TrueName格式不正确。");
        }

        /// <summary>
        /// 验证条件参数的有效性
        /// </summary>
        private void CheckConditionArgument(int ID)
        {
        }

        #endregion
    }
}
